package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.activity.client.ActivittyFeignClient;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.activity.SeckillGoods;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;

/**
 * @author Fxz
 * @version 1.0
 * @date 2022-02-25 16:30
 */
@Controller
@RequiredArgsConstructor
public class SeckillController {

	private final ActivittyFeignClient activittyFeignClient;

	@GetMapping("seckill.html")
	public String seckill(Model model) {
		Result<List<SeckillGoods>> result = activittyFeignClient.findAll();
		model.addAttribute("list", result.getData());
		return "seckill/index";
	}

	@GetMapping("/seckill/{skuId}.html")
	public String seckillItem(@PathVariable("skuId") Long skuId, Model model) {
		Result<SeckillGoods> result = activittyFeignClient.getSeckillGoods(skuId);
		model.addAttribute("item", result.getData());
		return "seckill/item";
	}

	@GetMapping("/seckill/queue.html")
	public String queue(HttpServletRequest request) {
		String skuId = request.getParameter("skuId");
		String skuIdStr = request.getParameter("skuIdStr");
		request.setAttribute("skuId", skuId);
		request.setAttribute("skuIdStr", skuIdStr);
		return "seckill/queue";
	}

	@GetMapping("/seckill/trade.html")
	public String trade(Model model) {
		Result<HashMap<String, Object>> result = activittyFeignClient.trade();
		if (result.isOk()) {
			model.addAllAttributes(result.getData());
			return "seckill/trade";
		}
		else {
			model.addAttribute("message", "下单失败");
			return "seckill/fail";
		}

	}

}
